package concurrent;

import org.assertj.core.api.ThrowableAssert;

import java.sql.PreparedStatement;
import java.util.Random;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author Summerday
 */
public class TestConcurrentLinkedQueue {

    static ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<>();

    public static void main (String[] args) throws InterruptedException {

        for (int i = 0; i < 10; i++) {
            Thread consumer = new Thread(() -> {
                while (true) {
                    Integer product = queue.poll();
                    if (product != null) {
                        System.out.println(Thread.currentThread().getName() + "消费产品 id - " + product);
                        return;
                    }
                }
            }, "consumer - " + i);
            consumer.setDaemon(true);
            consumer.start();
        }

        TimeUnit.SECONDS.sleep(1);

        Thread producer = new Thread(() -> {
            int product = new Random().nextInt(500);
            System.out.println(Thread.currentThread().getName() + "生产产品 id - " + product);
            queue.offer(product);

        }, "producer");

        producer.start();



    }
}
